package grpc

import (
	"context"
	"time"

	pb "vdrift/pkg/proto"

	"google.golang.org/grpc"
)

type Client struct {
	conn *grpc.ClientConn
	cli  pb.VdriftClient
}

func NewClient(target string) (*Client, error) {
	conn, err := grpc.NewClient(target, grpc.WithInsecure(), grpc.WithTimeout(2*time.Second))
	if err != nil {
		return nil, err
	}
	return &Client{conn: conn, cli: pb.NewVdriftClient(conn)}, nil
}

func (c *Client) Close() {
	c.conn.Close()
}

func (c *Client) SendState(state *pb.NodeState) {
	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
	defer cancel()
	c.cli.ReportState(ctx, state)
}
